//
//  ZYHomeWorkManagerCourseView.m
//  优学荟管理系统
//
//  Created by 星道 on 2017/9/28.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "ZYHomeWorkManagerCourseView.h"
#import "ZYHomeWorkManagerCourseCell.h"

#define RowWidth SCREEN_WIDTH*0.7
#define RowHeight 44*KScale
#define MaxHeight SCREENH_HEIGHT-64
@implementation ZYHomeWorkManagerCourseView
{
    NSArray<ZYHomeWorkManagerModel *>*_Arr;
    void (^_Complete)(NSInteger Index);

    // 显示的控件view
    UIView *_ContentView;
    // 表单
    UITableView *_tableView;
}

+ (instancetype)ConfigWithArr:(NSArray<ZYHomeWorkManagerModel *> *)Arr  Complete:(void (^)(NSInteger Index))Complete {

    return [[self alloc] initWithArr:Arr Complete:Complete];
}

- (instancetype)initWithArr:(NSArray<ZYHomeWorkManagerModel *> *)Arr  Complete:(void (^)(NSInteger Index))Complete {

    self = [super init];
    if (self) {

        _Arr = Arr;
        _Complete = Complete;
        self.backgroundColor = ColorRGBA(0, 0, 0, 0.65);
        self.frame = [UIScreen mainScreen].bounds;
        [[UIApplication sharedApplication].keyWindow addSubview:self];

        // 1、得到view的高度
        CGFloat height = [self ComputeHeight];
        // 2、绘制中间view的显示范围,同时创建出相应的指针空间
        [self ConfigView:height];
    }
    return self;
}






// 1、先搞清楚整个控件的高度
- (CGFloat)ComputeHeight {

    NSInteger count = _Arr.count;
    // 数据的高度再加上标栏的高度
    CGFloat height = (count * RowHeight) + RowHeight;
    // 判断下高度有没有超出屏幕高度,并回传出高度
    if (height < MaxHeight) {

        return height;
    } else {

        return MaxHeight;
    }
}

// 2、绘制中间view的显示范围,同时创建出相应的指针空间
- (void)ConfigView:(CGFloat)Height {

    // 白色的背景
    _ContentView = [[UIView alloc] init];
    _ContentView.backgroundColor = [UIColor whiteColor];
    _ContentView.frame = CGRectMake(0, 0, RowWidth, Height);
    _ContentView.center = CGPointMake(SCREEN_WIDTH/2, SCREENH_HEIGHT/2);
    [self addSubview:_ContentView];

    // 标题栏的view
    UIView *LabelView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _ContentView.frame.size.width, RowHeight)];
    [_ContentView addSubview:LabelView];

    // 蓝色线
    UIView *BlueLine = [[UIView alloc] initWithFrame:CGRectMake(0, LabelView.frame.size.height-2, LabelView.frame.size.width, 2)];
    BlueLine.backgroundColor = [UIColor blueColor];
    [LabelView addSubview:BlueLine];

    // 标题
    UILabel *Title = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, LabelView.frame.size.width-16, LabelView.frame.size.height-BlueLine.size.height)];
    Title.text = @"请选择开课课程";
    Title.textColor = BlueLine.backgroundColor;
    [LabelView addSubview:Title];

    // 表单
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LabelView.frame.size.height, _ContentView.frame.size.width, _ContentView.frame.size.height-LabelView.frame.size.height) style:(UITableViewStylePlain)];
    _tableView.tableHeaderView = [UIView new];
    _tableView.tableFooterView = [UIView new];
    [_tableView registerNib:[UINib nibWithNibName:[ZYHomeWorkManagerCourseCell description] bundle:nil] forCellReuseIdentifier:@"Cell"];
    _tableView.rowHeight = RowHeight;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_ContentView addSubview:_tableView];
}


#pragma mark - 表单代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _Arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ZYHomeWorkManagerCourseCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    Cell.model = _Arr[indexPath.row];
    return Cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    ZYHomeWorkManagerModel *Model = _Arr[indexPath.row];
    Model.selected = !Model.selected;
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationAutomatic)];
    _Complete ? _Complete (indexPath.row):nil;
    [self removeFromSuperview];
}
@end
